package com.example.mvpdemo.simple5.base;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;

/**
 * 抽象——>抽象出解绑和绑定操作
 * 注意：为了能够兼容多个模块，兼容多个Activity，所以采用泛型设计
 */
public abstract class BaseActivity<V extends BaseView_5, P extends BasePresenter_5<V>> extends Activity {


    private P presenter;
    private V view;
    private static final String TAG = "BaseActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (this.presenter == null) {
            this.presenter = createPresenter();
            Log.d(TAG, "onCreate: presenter");
        }

        if (this.view == null) {
            this.view = createView();
            Log.d(TAG, "onCreate: view");
        }

        if (this.view != null && this.presenter != null) {
            this.presenter.attachView(this.view);
            Log.d(TAG, "onCreate: attachView");
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.view != null && this.presenter != null) {
            this.presenter.detachView();
            Log.d(TAG, "onDestroy: ");
        }
    }

    public P getPresenter() {
        return presenter;
    }

    public abstract P createPresenter();

    public abstract V createView();

}
